package info.marlan.sim;

import info.marlan.sim.util.DBHelper;
import info.marlan.sim.util.DBHelper.Objective;
import info.marlan.sim.util.DBHelper.ObjectiveCategory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

public class ObjectiveDialog extends ExpandableListActivity{
    public static final String NAME = "NAME";
    private static final String IS_EVEN = "IS_EVEN";
    private ExpandableListAdapter mAdapter;
    private DBHelper dbHelper;
    private SharedPreferences preferences;
    private List<Map<String, String>> groupData ;
    private List<List<Map<String, String>>> childData;
    private String languagecode;
    private Editor preferencesEditor;
    private boolean freetextAnswers;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        preferences =
        	ObjectiveDialog.this.getSharedPreferences(
        	Constants.PREFERENCES, Context.MODE_PRIVATE);
        dbHelper = new DBHelper(ObjectiveDialog.this);

        groupData = new ArrayList<Map<String, String>>();
        childData = new ArrayList<List<Map<String, String>>>();
        languagecode = preferences.getString(Constants.PREFERENCES_LANGUAGE,Constants.PREFERENCES_DEFAULT_LANGUAGE);
        freetextAnswers = preferences.getBoolean(Constants.PREFERENCES_ENABLE_FREETEXT_ANSWERS, Constants.PREFERENCES_DEFAULT_ENABLE_FREETEXT_ANSWERS);
        
        String randomTitle = null;
        String availableQuestions;
        if(Constants.PREFERENCES_LANGUAGE_CODE_DE.equals(languagecode)){
        	randomTitle = getString(R.string.random_question_de);
        	availableQuestions = getString(R.string.question_available_de);
        }else{
        	randomTitle = getString(R.string.random_question_en);
        	availableQuestions = getString(R.string.question_available_en);
        }
        List<ObjectiveCategory> objectiveCategories = dbHelper.getObjectiveCategories("languagecode = ?",
        		new String[]{preferences.getString(Constants.PREFERENCES_LANGUAGE,Constants.PREFERENCES_DEFAULT_LANGUAGE)},"category");
        for (ObjectiveCategory objectiveCategory:objectiveCategories) {
        	
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, objectiveCategory.category + " ("+ objectiveCategory.questions +" "+ availableQuestions+")" );
            curGroupMap.put(IS_EVEN, objectiveCategory.getShortenTitle());
            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            List<Objective> objectives = dbHelper.getObjective("objective_category = ?",new String[]{""+objectiveCategory.id},"category,sort" );
            if(objectiveCategory.questions > 10){
            Map<String, String> randomChildMap = new HashMap<String, String>();
            children.add(randomChildMap );
            String subcategory = (objectiveCategory.category.startsWith("1") ? "1.":"2.") + objectiveCategory.category;
            randomChildMap.put(NAME,subcategory);
            randomChildMap.put(IS_EVEN, randomTitle +" "+ subcategory);
            }
            for (Objective objective:objectives) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
                curChildMap.put(NAME,objective.category);
                curChildMap.put(IS_EVEN, objective.getShortenTitle());
            }
            childData.add(children);
        }
     
        // Set up our adapter
        mAdapter = new SimpleExpandableListAdapter(
                this,
                groupData,
                android.R.layout.simple_expandable_list_item_2,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 },
                childData,
                android.R.layout.simple_expandable_list_item_2,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 }
                );
        setListAdapter(mAdapter);
        dbHelper.cleanup();
    }


	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		
		Log.v(Constants.LOGTAG,childData.get(groupPosition).get(childPosition).toString());
		Intent i = new Intent(getApplication(),Test.class);
		i.putExtra(NAME, childData.get(groupPosition).get(childPosition).get(NAME));
		startActivity(i);
		return super.onChildClick(parent, v, groupPosition, childPosition, id);
	}
	
	
	public boolean onCreateOptionsMenu(Menu menu) {
	
		SubMenu preferencesMenu = menu.addSubMenu(
				Constants.PREFERENCES_LANGUAGE_CODE_DE.equals(languagecode)?
						R.string.preferences_de:R.string.preferences_en).setIcon(R.drawable.settings);

		MenuItem item1 = preferencesMenu.add(Constants.RB_GROUP,Constants.RADIOBUTTON_1, Menu.NONE, getResources().getStringArray(R.array.languages)[0]);
		MenuItem item2 = preferencesMenu.add(Constants.RB_GROUP, Constants.RADIOBUTTON_2, Menu.NONE, getResources().getStringArray(R.array.languages)[1]);
		MenuItem item3 = preferencesMenu.add(Constants.CHECKBOX, Constants.CHECKBOX_1, Menu.NONE, getResources().getString(Constants.PREFERENCES_LANGUAGE_CODE_DE.equals(languagecode)?R.string.enable_freetext_de:R.string.enable_freetext_en));
		
		MenuItem item4 = preferencesMenu.add(6, 6, Menu.NONE,Constants.PREFERENCES_LANGUAGE_CODE_DE.equals(languagecode)?
				R.string.close_de:R.string.close_en);
		
		item3.setCheckable(true);
		
		if(freetextAnswers){
			item3.setChecked(true);
		}else{
			item3.setChecked(false);
		}
		
		
		if(Constants.PREFERENCES_LANGUAGE_CODE_DE.equals(languagecode)){
			item1.setChecked(true);
		}else{
			item2.setChecked(true);
		}
		
		item1.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			  public boolean onMenuItemClick(MenuItem _menuItem) {
				    preferencesEditor = ObjectiveDialog.this.preferences.edit();
                    preferencesEditor.putString(Constants.PREFERENCES_LANGUAGE, Constants.PREFERENCES_LANGUAGE_CODE_DE);
                	preferencesEditor.commit();  
                	startActivity(new Intent(getApplication(),ObjectiveDialog.class));
                	ObjectiveDialog.this.finish();
			    return true;
			  }
			});

		item2.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			  public boolean onMenuItemClick(MenuItem _menuItem) {
				    preferencesEditor = ObjectiveDialog.this.preferences.edit();
                    preferencesEditor.putString(Constants.PREFERENCES_LANGUAGE, Constants.PREFERENCES_LANGUAGE_CODE_EN);
                	preferencesEditor.commit();  
                	startActivity(new Intent(getApplication(),ObjectiveDialog.class));
                	ObjectiveDialog.this.finish();
			    return true;
			  }
			});
		
		item3.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			  public boolean onMenuItemClick(MenuItem _menuItem) {
				  
				  freetextAnswers = !_menuItem.isChecked();
				    preferencesEditor = ObjectiveDialog.this.preferences.edit();
				    preferencesEditor.putBoolean(Constants.PREFERENCES_ENABLE_FREETEXT_ANSWERS, freetextAnswers);
				    preferencesEditor.commit();  
				    _menuItem.setChecked(freetextAnswers);
			    return true;
			  }
			});
		
		preferencesMenu.setGroupCheckable(Constants.RB_GROUP, true, true);
		
	    return true;
	}


    
}
